package com.web.manager.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.mapper.CourtMapper;
import com.web.manager.mapper.MajorMapper;
import com.web.manager.pojo.Court;
import com.web.manager.pojo.Major;
import com.web.manager.pojo.Result;
import com.web.manager.service.MajorService;
import com.web.manager.pojo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/major")
public class MajorController {
    @Resource
    private MajorService majorService;

    @Resource
    private MajorMapper majorMapper;

    @Resource
    private CourtMapper courtMapper;

    @GetMapping("/{majorid}")
    public Result getById(@PathVariable Integer majorid){

        Major dept =majorService.getByMajorid(majorid);
        return Result.success(dept);
    }

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody Major major){
        majorService.save(major);  //insertUser//save==>插入一个信息新增
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Major major){
        majorService.updateById(major);  //updateUser
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        Major major = majorMapper.selectById2(id);
        int a  = courtMapper.selectType(major.getBelong());
        Court court = courtMapper.selectType2(major.getBelong());
        court.setNum(a-1);
        courtMapper.updateById(court);

        majorService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        for (Integer id: ids) {

            Major major =majorMapper.selectById2(id);
            int a  = majorMapper.selectType(major.getBelong());
            Court court = courtMapper.selectType2(major.getBelong());
            court.setNum(a-1);
            courtMapper.updateById(court);
            majorService.removeById(id);

        }

        //batchDeleteUser
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Major> userList = majorService.list();  //selectAll
        return Result.success(userList);
    }



    //根据id查询
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){

        Major major= majorService.getById(id);   //selectById
        return Result.success(major);
    }



    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String name) {

        QueryWrapper<Major> queryWrapper = new QueryWrapper<Major>();  //条件构造器
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
        //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

        Page<Major> page = majorService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }
}
